package com.huawei.appmarket.support.audio;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.SparseArray;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.FrameLayout;
import com.huawei.appgallery.foundation.service.common.protocol.AppDetailActivityProtocol;
import com.huawei.appgallery.foundation.service.common.protocol.AppDetailActivityProtocol.Request;
import com.huawei.appgallery.foundation.ui.framework.activity.BaseActivity;
import java.lang.ref.WeakReference;
import o.cjk;
import o.cjm;
import o.cjo;
import o.dgm;
import o.dky.h;
import o.Logger;
import o.epd;
import o.epe;
import o.kp;
import o.kp.e;
import o.ks;
import o.kv;

public class MainViewController implements ks, OnClickListener {
    private int ˊ = -1;
    private SparseArray<WeakReference<BaseActivity>> ˋ = new SparseArray(3);
    private epd ˏ;
    private SparseArray<WeakReference<AudioPlayFloatButton>> ॱ = new SparseArray(3);

    static class c implements epd {
        private WeakReference<MainViewController> ˊ;

        public c(MainViewController mainViewController) {
            this.ˊ = new WeakReference(mainViewController);
        }

        public void ˊ(AudioBean audioBean) {
            MainViewController mainViewController = (MainViewController) this.ˊ.get();
            if (mainViewController != null) {
                mainViewController.ॱ(audioBean);
            }
        }

        public void ˊ(int i) {
        }

        public void ॱ(int i, int i2) {
            MainViewController mainViewController = (MainViewController) this.ˊ.get();
            if (mainViewController != null) {
                mainViewController.ˊ();
            }
        }

        public void ˋ(int i) {
        }

        public void ˎ() {
            MainViewController mainViewController = (MainViewController) this.ˊ.get();
            if (mainViewController != null) {
                mainViewController.ˏ();
            }
        }

        public void ˋ() {
            MainViewController mainViewController = (MainViewController) this.ˊ.get();
            if (mainViewController != null) {
                mainViewController.ˏ();
            }
        }

        public void ˋ(int i, String str) {
            MainViewController mainViewController = (MainViewController) this.ˊ.get();
            if (mainViewController != null) {
                mainViewController.ˏ();
            }
        }
    }

    public void ॱ(int i, BaseActivity baseActivity, Bundle bundle) {
        if (baseActivity != null) {
            this.ˋ.put(i, new WeakReference(baseActivity));
            if (epe.ˏ().ॱॱ(i)) {
                epe.ˏ().ˎ(i);
                epe.ˏ().ˎ(i, false);
            }
            kp ac_ = baseActivity.ac_();
            if (ac_ != null) {
                ac_.ॱ(this);
            }
        }
    }

    private void ˏ(BaseActivity baseActivity) {
        Object obj;
        View findViewById = baseActivity.findViewById(h.ˎ);
        AudioBean ॱ = epe.ˏ().ॱ();
        if (findViewById instanceof AudioPlayFloatButton) {
            obj = (AudioPlayFloatButton) findViewById;
            obj.setVisibility(0);
            AudioBean ˎ = obj.ˎ();
            if (!(ˎ == null || ˎ.equals(ॱ))) {
                obj.setData(ॱ);
            }
            obj.ˊ();
        } else {
            findViewById = baseActivity.findViewById(16908290);
            if (findViewById instanceof FrameLayout) {
                findViewById = ((FrameLayout) findViewById).getChildAt(0);
                if (findViewById instanceof FrameLayout) {
                    FrameLayout frameLayout = (FrameLayout) findViewById;
                    int dimensionPixelSize = baseActivity.getResources().getDimensionPixelSize(o.dky.c.ʾ);
                    int dimensionPixelSize2 = baseActivity.getResources().getDimensionPixelSize(o.dky.c.ˉ) + baseActivity.getResources().getDimensionPixelSize(o.dky.c.ʼ);
                    int dimensionPixelSize3 = baseActivity.getResources().getDimensionPixelSize(o.dky.c.ˋ);
                    LayoutParams layoutParams = new FrameLayout.LayoutParams(dimensionPixelSize, dimensionPixelSize);
                    layoutParams.gravity = 8388693;
                    layoutParams.bottomMargin = dimensionPixelSize2;
                    layoutParams.setMarginEnd(dimensionPixelSize3);
                    View audioPlayFloatButton = new AudioPlayFloatButton(baseActivity);
                    audioPlayFloatButton.setId(h.ˎ);
                    audioPlayFloatButton.setGravity(17);
                    frameLayout.addView(audioPlayFloatButton, layoutParams);
                    audioPlayFloatButton.setOnClickListener(this);
                    audioPlayFloatButton.setData(ॱ);
                    audioPlayFloatButton.ˊ();
                    findViewById = audioPlayFloatButton;
                }
            }
            obj = null;
        }
        if (obj != null) {
            this.ॱ.put(dgm.ˎ((Activity) baseActivity), new WeakReference(obj));
        }
    }

    private void ˋ(BaseActivity baseActivity) {
        if (baseActivity != null) {
            View findViewById = baseActivity.findViewById(h.ˎ);
            if (findViewById instanceof AudioPlayFloatButton) {
                AudioPlayFloatButton audioPlayFloatButton = (AudioPlayFloatButton) findViewById;
                audioPlayFloatButton.setVisibility(8);
                audioPlayFloatButton.ˏ();
            }
        }
    }

    private void ˏ() {
        WeakReference weakReference = (WeakReference) this.ॱ.get(this.ˊ);
        if (weakReference != null) {
            AudioPlayFloatButton audioPlayFloatButton = (AudioPlayFloatButton) weakReference.get();
            if (audioPlayFloatButton != null) {
                audioPlayFloatButton.setVisibility(8);
                audioPlayFloatButton.ˏ();
            }
        }
    }

    private void ˊ() {
        AudioBean ॱ = epe.ˏ().ॱ();
        if (ॱ != null) {
            WeakReference weakReference = (WeakReference) this.ॱ.get(ॱ.ॱॱ());
            if (weakReference != null) {
                AudioPlayFloatButton audioPlayFloatButton = (AudioPlayFloatButton) weakReference.get();
                if (audioPlayFloatButton != null) {
                    audioPlayFloatButton.ˋ();
                }
            }
        }
    }

    public void ॱ() {
        if (this.ˏ == null) {
            this.ˏ = new c(this);
        }
        epe.ˏ().ˋ(this.ˏ);
    }

    public void ˋ() {
        epe.ˏ().ˊ(this.ˏ);
    }

    public void ˊ(kv kvVar, e eVar) {
        if (kvVar instanceof BaseActivity) {
            BaseActivity baseActivity = (BaseActivity) kvVar;
            int ˎ = dgm.ˎ((Activity) baseActivity);
            switch (eVar) {
                case ON_RESUME:
                    if (epe.ˏ().ॱ(ˎ)) {
                        ˏ(baseActivity);
                    } else {
                        ˋ(baseActivity);
                    }
                    ॱ();
                    this.ˊ = ˎ;
                    return;
                case ON_PAUSE:
                    ˋ();
                    this.ˊ = -1;
                    return;
                case ON_DESTROY:
                    WeakReference weakReference = (WeakReference) this.ˋ.get(ˎ);
                    if (weakReference != null && baseActivity == weakReference.get()) {
                        this.ˋ.remove(ˎ);
                    }
                    if (this.ˋ.size() <= 0) {
                        ˋ();
                    }
                    kp ac_ = baseActivity.ac_();
                    if (ac_ != null) {
                        ac_.ˋ(this);
                        return;
                    }
                    return;
                default:
                    return;
            }
        }
    }

    public void ॱ(AudioBean audioBean) {
        int i;
        if (audioBean == null || !audioBean.ˋॱ()) {
            i = -1;
        } else {
            i = audioBean.ॱॱ();
        }
        int size = this.ˋ.size();
        for (int i2 = 0; i2 < size; i2++) {
            WeakReference weakReference = (WeakReference) this.ˋ.valueAt(i2);
            if (weakReference != null) {
                BaseActivity baseActivity = (BaseActivity) weakReference.get();
                if (baseActivity != null) {
                    if (dgm.ˎ((Activity) baseActivity) == i) {
                        ˏ(baseActivity);
                    } else {
                        ˋ(baseActivity);
                    }
                }
            }
        }
    }

    public void onClick(View view) {
        AudioBean ॱ = epe.ˏ().ॱ();
        if (ॱ != null) {
            ˊ(view.getContext(), ॱ.ˏ(), ॱ.ʻ());
        }
    }

    public void ˊ(Context context, String str, String str2) {
        if (Logger.ˎ()) {
            Logger.ˏ("MainViewController", "start detail");
        }
        cjo appDetailActivityProtocol = new AppDetailActivityProtocol();
        appDetailActivityProtocol.ˏ(new Request(str, str2));
        cjk.ˎ().ˋ(context, new cjm("appdetail.activity", appDetailActivityProtocol));
    }
}
